{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyMuPKD8A+TQOCFtEFBbYKge",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/nceder/qpb4e/blob/main/code/Chapter%2005/Chapter_05.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 5 Lists, tuples, and sets\n",
        "\n",
        "# 5.1 Lists are like arrays"
      ],
      "metadata": {
        "id": "GTZms2Y6Hr-t"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "-sgAYzHS1_aQ"
      },
      "outputs": [],
      "source": [
        "# This assigns a three-element list to x\n",
        "x = [1, 2, 3]"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# First element is a number, second is a string, third is another list.\n",
        "x = [2, \"two\", [1, 2, 3]]\n",
        "len(x)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8tReATEQ2E-A",
        "outputId": "a78f27c3-3f03-4332-da58-378d1f3f0cfc"
      },
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "3"
            ]
          },
          "metadata": {},
          "execution_count": 2
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: len()\n",
        "What would len() return for each of the following: `[0]`; `[]`; `[[1, 3, [4, 5], 6], 7]`?"
      ],
      "metadata": {
        "id": "--IS5FhQIROL"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "len([0])"
      ],
      "metadata": {
        "id": "MLkieUClIjRN"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "len([])"
      ],
      "metadata": {
        "id": "t_Dpn0clImGr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "len([[1, 3, [4, 5], 6], 7])"
      ],
      "metadata": {
        "id": "unOIv3aYIo56"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 5.2 List indices"
      ],
      "metadata": {
        "id": "6uDBMySWIsyh"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = [\"first\", \"second\", \"third\", \"fourth\"]\n",
        "x[0]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "PUBc5Y9-2Jnb",
        "outputId": "c060aa55-1430-4b1c-9a81-b2646bd8b7b9"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'first'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[2]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "WmUJLIY-6iXX",
        "outputId": "8fe53da2-8836-416c-f0f8-2bc0c4616a5b"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'third'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[-2]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "WpACR2hQ5hCf",
        "outputId": "b7dacf12-d4fa-49d0-8647-a5912af40d81"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'third'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [\"first\", \"second\", \"third\", \"fourth\"]\n",
        "x[1:-1]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qcq-S7Rb5kXM",
        "outputId": "28fb79b2-aba8-46ec-927a-3df04a904e83"
      },
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['second', 'third']"
            ]
          },
          "metadata": {},
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[0:3]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qJoi1WWl6Yir",
        "outputId": "2534ab0c-1bd1-4591-d414-ee049299b871"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['first', 'second', 'third']"
            ]
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[2:]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WEQfCzbQ8N1G",
        "outputId": "737e27f3-fe65-4f33-8200-46e4c1bddc67"
      },
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['third', 'fourth']"
            ]
          },
          "metadata": {},
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "y = x[:]\n",
        "y[0] = '1 st'\n",
        "y"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XbB_26i58PkH",
        "outputId": "268b19c0-7c7e-4a34-ea76-be58944ae5f9"
      },
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['1 st', 'second', 'third', 'fourth']"
            ]
          },
          "metadata": {},
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-h3lMkbg8TnW",
        "outputId": "a79bf7f9-def8-4e10-8296-02746ff9fca1"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['first', 'second', 'third', 'fourth']"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Try This: list slices and indexes\n",
        "Using what you know about the `len()` function and list slices, how would you combine the two to get the second half of a list when you don't know what size it is? Experiment in the Python shell to confirm that your solution works."
      ],
      "metadata": {
        "id": "J6k8LKDUI27l"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# experiment with your code here\n"
      ],
      "metadata": {
        "id": "LvLhhKeZJCyR"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "my_list = [1, 2, 3, 4, 5, 6]\n",
        "last_half = my_list[len(my_list)//2:]\n",
        "last_half"
      ],
      "metadata": {
        "id": "dkoYUSaKJJVQ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 5.3 Modifying lists"
      ],
      "metadata": {
        "id": "MK49yF5AJXWe"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 3, 4]\n",
        "x[1] = \"two\"\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-XFGvIwH8WK1",
        "outputId": "55ecf64e-4264-40af-f0c6-dab5e4f97b2d"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 'two', 3, 4]"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 3, 4]\n",
        "x[len(x):] = [5, 6, 7]              #A\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kdYbKIuQ8ZOk",
        "outputId": "6d01e10d-d7b2-4b87-8ee5-9d4c6d045a4f"
      },
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 4, 5, 6, 7]"
            ]
          },
          "metadata": {},
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[:0] = [-1, 0]                     #B\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OM-BecYi8bfv",
        "outputId": "651963e5-9ba0-4d18-b9a1-1435f2d550a1"
      },
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[-1, 0, 1, 2, 3, 4, 5, 6, 7]"
            ]
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[1:-1] = []                        #C\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "P3r1um4A8dR_",
        "outputId": "f308b224-9c52-44aa-ae74-48ebd849ed64"
      },
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[-1, 7]"
            ]
          },
          "metadata": {},
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 3]\n",
        "x.append(\"four\")\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ci9Brng_8gZA",
        "outputId": "2fa700da-ea96-40af-f24f-69078c9d4055"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 'four']"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 3, 4]\n",
        "y = [5, 6, 7]\n",
        "x.append(y)\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zHawagwX8itx",
        "outputId": "7a3080b1-bed3-4114-b5a0-05ccd0465016"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 4, [5, 6, 7]]"
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 3, 4]\n",
        "y = [5, 6, 7]\n",
        "x.extend(y)\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "alY8EN2U8lIv",
        "outputId": "5e8252c5-562b-4059-fc3d-0fd2513d3bf5"
      },
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 4, 5, 6, 7]"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 3]\n",
        "x.insert(2, \"hello\")\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MprQsVCk8nfj",
        "outputId": "faa402ec-86cc-4bb5-9119-4e8936d27cd6"
      },
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 'hello', 3]"
            ]
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.insert(0, \"start\")\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IAaJGSpL8qEL",
        "outputId": "026404af-e388-4821-d528-40aaffbfc54a"
      },
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['start', 1, 2, 'hello', 3]"
            ]
          },
          "metadata": {},
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 3]\n",
        "x.insert(-1, \"hello\")\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "UBPYNUtZ8y-b",
        "outputId": "c599dac2-adc7-4c95-b95e-540e473eb988"
      },
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 'hello', 3]"
            ]
          },
          "metadata": {},
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = ['a', 2, 'c', 7, 9, 11]\n",
        "del x[1]\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "xDN9l9zE88lT",
        "outputId": "62dc677e-3233-478c-95df-8459c982798f"
      },
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['a', 'c', 7, 9, 11]"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "del x[:2]\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "YcLzxlcO8_Xf",
        "outputId": "bb158bdf-abdd-4ffc-d00c-721f765ccd17"
      },
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[7, 9, 11]"
            ]
          },
          "metadata": {},
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 3, 4, 3, 5]\n",
        "x.remove(3)\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "frfLq_vg9Bir",
        "outputId": "d814cc5d-1855-49ec-c3d2-36359cd96ea9"
      },
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 4, 3, 5]"
            ]
          },
          "metadata": {},
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.remove(3)\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fyDHGqEX9EGj",
        "outputId": "8d85fd2b-fd01-4660-95de-feda54a51b20"
      },
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 4, 5]"
            ]
          },
          "metadata": {},
          "execution_count": 28
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.remove(3)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "hV5LgiDX9GMh",
        "outputId": "68b9fabb-e2a5-4e4f-9f95-5fadfe8c3388"
      },
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "list.remove(x): x not in list",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-29-c4efe2720efe>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 3, 5, 6, 7]\n",
        "x.reverse()\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BLMGOJ9z9IaN",
        "outputId": "c7d90752-a5a3-460f-9239-6ef0a3f3b3bb"
      },
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[7, 6, 5, 3, 1]"
            ]
          },
          "metadata": {},
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Try this: Modifying lists\n",
        "Suppose that you have a list 10 items long. How might you move the last three items from the end of the list to the beginning, keeping them in the same order?\n"
      ],
      "metadata": {
        "id": "aYRT-IuPJo6T"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# experiement here"
      ],
      "metadata": {
        "id": "gRF_22kUKKDd"
      },
      "execution_count": 31,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
        "my_list = my_list[-3:] + my_list[:-3]\n",
        "my_list"
      ],
      "metadata": {
        "id": "cE3Li-FYK0kg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 5.4 Sorting lists"
      ],
      "metadata": {
        "id": "cLfUP1FvK28O"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "my_list = my_list[-3:] + my_list[:-3]\n",
        "my_list"
      ],
      "metadata": {
        "id": "0PfSn481KX7B",
        "outputId": "ab9362db-1f0e-44a8-8154-1485fa82e395",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 34,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[5, 6, 7, 8, 9, 10, 1, 2, 3, 4]"
            ]
          },
          "metadata": {},
          "execution_count": 34
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [3, 8, 4, 0, 2, 1]\n",
        "x.sort()\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zdX9vFJE9MYb",
        "outputId": "4fb317e2-5ace-4cad-f030-deba969b1ff8"
      },
      "execution_count": 35,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[0, 1, 2, 3, 4, 8]"
            ]
          },
          "metadata": {},
          "execution_count": 35
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [2, 4, 1, 3]\n",
        "y = x[:]\n",
        "y.sort()\n",
        "y"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_a7bn_OH9Oc6",
        "outputId": "e446a2ff-f9a8-4407-9003-fa66cc5fced6"
      },
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 4]"
            ]
          },
          "metadata": {},
          "execution_count": 36
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Sf48v9uE9Qnc",
        "outputId": "5650c7eb-ec50-4a2b-b5e3-3b4e5868ab18"
      },
      "execution_count": 37,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[2, 4, 1, 3]"
            ]
          },
          "metadata": {},
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [\"Life\", \"Is\", \"Enchanting\"]\n",
        "x.sort()\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ges4oBy69SOy",
        "outputId": "37ff4061-2a49-4032-e5f8-e0aaedd95bf2"
      },
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['Enchanting', 'Is', 'Life']"
            ]
          },
          "metadata": {},
          "execution_count": 38
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 'hello', 3]\n",
        "x.sort()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 159
        },
        "id": "nWTynOdz9UNT",
        "outputId": "0646d698-2e70-44ff-c7e9-125d0084fa12"
      },
      "execution_count": 39,
      "outputs": [
        {
          "output_type": "error",
          "ename": "TypeError",
          "evalue": "'<' not supported between instances of 'str' and 'int'",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-39-ff66ee7b4234>\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'hello'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m: '<' not supported between instances of 'str' and 'int'"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [[3, 5], [2, 9], [2, 3], [4, 1], [3, 2]]\n",
        "x.sort()\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZgfSgIQ09W8F",
        "outputId": "cedbeb0d-f275-41ed-b9fc-a7c76a8a8267"
      },
      "execution_count": 40,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[[2, 3], [2, 9], [3, 2], [3, 5], [4, 1]]"
            ]
          },
          "metadata": {},
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "def comp_num_of_chars(string1):\n",
        "    return len(string1)\n",
        "word_list = ['Python', 'is', 'better', 'than', 'C']\n",
        "word_list.sort()\n",
        "print(word_list)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "P4TTCfNy9ZR8",
        "outputId": "06bdcc16-fd5b-4957-a20d-79bce1598bca"
      },
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "['C', 'Python', 'better', 'is', 'than']\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "word_list = ['Python', 'is', 'better', 'than', 'C']\n",
        "word_list.sort(key=comp_num_of_chars)\n",
        "print(word_list)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "noSvnryH9eOk",
        "outputId": "c340c531-74f0-4a36-ce78-ed693dccc8dc"
      },
      "execution_count": 42,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "['C', 'is', 'than', 'Python', 'better']\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = (4, 3, 1, 2)\n",
        "y = sorted(x)\n",
        "y"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "D5-5ZbED9hBn",
        "outputId": "a71f5736-69a6-4483-ae14-76e5aca6ae54"
      },
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 4]"
            ]
          },
          "metadata": {},
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "z = sorted(x, reverse=True)\n",
        "z"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "s3nIlIHN9kUK",
        "outputId": "c24aa1e2-f2d9-40f7-8427-dd5be33c64fe"
      },
      "execution_count": 44,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[4, 3, 2, 1]"
            ]
          },
          "metadata": {},
          "execution_count": 44
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Try this: Sorting lists\n",
        "Suppose that you have a list in which each element is in turn a list: `[[1, 2, 3], [2, 1, 3], [4, 0, 1]]`. If you wanted to sort this list by the second element in each list so that the result would be `[[4, 0, 1], [2, 1, 3], [1, 2, 3]]`, what function would you write to pass as the key value to the sort() method?"
      ],
      "metadata": {
        "id": "vW-_r_w-LDHd"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# experiement here"
      ],
      "metadata": {
        "id": "wywIsO_mLOrN"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "the_list =  [[1, 2, 3], [2, 1, 3], [4, 0, 1]]\n",
        "the_list.sort(key=lambda x: x[1])\n",
        "the_list"
      ],
      "metadata": {
        "id": "-NbzwCVELO5J"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "or"
      ],
      "metadata": {
        "id": "JjpAPlwxMICc"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "the_list =  [[1, 2, 3], [2, 1, 3], [4, 0, 1]]\n",
        "the_list.sort(key=lambda x: x[1])\n",
        "the_list"
      ],
      "metadata": {
        "id": "d7Dab0DbMDIp"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 5.5 Other list operations"
      ],
      "metadata": {
        "id": "QGW0XllCMJf8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "3 in [1, 3, 4, 5]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "P0uMZHvM9nAI",
        "outputId": "45d169bf-f5f7-4ccf-aa58-77a74de89470"
      },
      "execution_count": 45,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 45
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "3 not in [1, 3, 4, 5]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EMSc5-QX9pQQ",
        "outputId": "081b8cc9-7faa-4e1c-eebd-073fc458ff31"
      },
      "execution_count": 46,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 46
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "3 in [\"one\", \"two\", \"three\"]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IL7RvAks9rgT",
        "outputId": "d8d4428c-6ef4-4872-ac77-51804c965fd1"
      },
      "execution_count": 47,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 47
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "3 not in [\"one\", \"two\", \"three\"]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dIKRb6lM9tpO",
        "outputId": "2c64d281-a058-4c00-bfeb-e9f33210cd2b"
      },
      "execution_count": 48,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 48
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "z = [1, 2, 3] + [4, 5]\n",
        "z"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "b6QkMPGu9vPH",
        "outputId": "1c3697cf-a543-4fd7-ce78-9d2b76fdce58"
      },
      "execution_count": 49,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 4, 5]"
            ]
          },
          "metadata": {},
          "execution_count": 49
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "z = [None] * 4\n",
        "z"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GT0tqHBK9xjH",
        "outputId": "f160fa40-e7a3-4e75-ec6b-33561b09d30b"
      },
      "execution_count": 50,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[None, None, None, None]"
            ]
          },
          "metadata": {},
          "execution_count": 50
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "z = [3, 1] * 2\n",
        "z"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "x_6dYvsv9zzP",
        "outputId": "df044483-41ec-40f9-de9a-6d09f54eeba2"
      },
      "execution_count": 51,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[3, 1, 3, 1]"
            ]
          },
          "metadata": {},
          "execution_count": 51
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "min([3, 7, 0, -2, 11])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lXWik2aH92A3",
        "outputId": "154b1d80-f084-4551-ba39-a721e41c91b0"
      },
      "execution_count": 52,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "-2"
            ]
          },
          "metadata": {},
          "execution_count": 52
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "max([4, \"Hello\", [1, 2]])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "ABR2Rxkh939W",
        "outputId": "bb2b7d2b-a77d-47b0-f618-ac9e24493819"
      },
      "execution_count": 53,
      "outputs": [
        {
          "output_type": "error",
          "ename": "TypeError",
          "evalue": "'>' not supported between instances of 'str' and 'int'",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-53-15ab1869d5d5>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Hello\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'str' and 'int'"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 3, \"five\", 7, -2]\n",
        "x.index(7)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HZuBr5xZ96sd",
        "outputId": "2c921c00-3bc7-40a4-aadf-af1016d89af3"
      },
      "execution_count": 54,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "3"
            ]
          },
          "metadata": {},
          "execution_count": 54
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.index(5)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "HxWTEBTn98ij",
        "outputId": "ac7adee0-6729-44c7-9b17-75d7d1ea449c"
      },
      "execution_count": 55,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "5 is not in list",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-55-96ad5df81983>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: 5 is not in list"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 2, 3, 5, 2, 5]\n",
        "x.count(2)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MrccfH999_nD",
        "outputId": "db33a9e0-a9a6-46a6-9c51-9d680a81ecdc"
      },
      "execution_count": 56,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "3"
            ]
          },
          "metadata": {},
          "execution_count": 56
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.count(5)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Qd4AcerO-CNa",
        "outputId": "0d6f4744-02fc-434b-d942-eaabfd712264"
      },
      "execution_count": 57,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2"
            ]
          },
          "metadata": {},
          "execution_count": 57
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.count(4)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "DWAco0hg-EWH",
        "outputId": "2809d52f-d22c-4286-ce13-30d5bb233b1a"
      },
      "execution_count": 58,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {},
          "execution_count": 58
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: List Operations\n",
        "What would be the result of `len([[1,2]] * 3)`?\n",
        "\n",
        "What are two differences between using the `in` operator and a list's `index()` method?\n",
        "\n",
        "Which of the following will raise an exception?: `min([\"a\", \"b”, \"c\"])`; `max([1, 2, \"three\"])`; `[1, 2, 3].count(\"one\")`"
      ],
      "metadata": {
        "id": "LD1W5yNmMs9q"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "len([[1,2]] * 3)"
      ],
      "metadata": {
        "id": "ZcKY5DBBM9tp"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "What are two differences between using the `in` operator and a list's `index()` method?\n",
        "\n",
        "* index gives position; in gives a true/false answer.\n",
        "* index gives an error if an element isn’t in the list.\n",
        "Which of the following raises an exception?"
      ],
      "metadata": {
        "id": "LF-Fg8JTNiFk"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "min([\"a\", \"b”, \"c\"])"
      ],
      "metadata": {
        "id": "Z4H1xdI9M932"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "max([1, 2, \"three\"])"
      ],
      "metadata": {
        "id": "2q4TuVIVNYv2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "[1, 2, 3].count(\"one\")"
      ],
      "metadata": {
        "id": "5ssHR5FTNchX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Try This: List operations\n",
        "If you have a list x, write the code to safely remove an item if—and only if—that value is in the list.\n",
        "\n",
        "Modify that code to remove the element only if the item occurs in the list more than once.\n"
      ],
      "metadata": {
        "id": "2tlJQKR7NtxN"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "kDAaBtXNOCbK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 3, 4, 5, 4]\n",
        "# change element to a value not in x to text counter case\n",
        "element = 3\n",
        "if element in x:\n",
        "    x.remove(element)\n",
        "x"
      ],
      "metadata": {
        "id": "jMzcpgN7OCxO",
        "outputId": "032026d4-4f41-4d50-d47b-124ac10d26d1",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 4, 5, 4]"
            ]
          },
          "metadata": {},
          "execution_count": 1
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 2, 3, 4, 5, 4]\n",
        "# change element to something other than 4 or a value not in x to text counter cases\n",
        "element = 4\n",
        "if x.count(element) > 1:\n",
        "    x.remove(element)\n",
        "x"
      ],
      "metadata": {
        "id": "EptVuhkIPF_X",
        "outputId": "eedf376d-0c6e-4edd-fa7d-dd56daa4daa3",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 4, 5, 4]"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 5.6 Nested lists and deep copies"
      ],
      "metadata": {
        "id": "osCfTNWeQL7Y"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "m = [[0, 1, 2], [10, 11, 12], [20, 21, 22]]\n",
        "m[0]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2KvOAj2d-Fyr",
        "outputId": "22d2ca7e-ed63-4bbd-ae27-f65317f01f45"
      },
      "execution_count": 59,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[0, 1, 2]"
            ]
          },
          "metadata": {},
          "execution_count": 59
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "m[0][1]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "G2a0KB3y-JQM",
        "outputId": "4a81c816-2247-491e-d1ef-4a5e725cf8a2"
      },
      "execution_count": 60,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {},
          "execution_count": 60
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "m[2]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bUyKmYX6-K1T",
        "outputId": "ec96d906-64db-46ac-fa68-96beacbc9964"
      },
      "execution_count": 61,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[20, 21, 22]"
            ]
          },
          "metadata": {},
          "execution_count": 61
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "m[2][2]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "y_xa-2sk-MsM",
        "outputId": "aac540af-ecfa-4489-9c06-2d0ca9c90590"
      },
      "execution_count": 62,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "22"
            ]
          },
          "metadata": {},
          "execution_count": 62
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "nested = [0]\n",
        "original = [nested, 1]\n",
        "original"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3sywvmbD-OCT",
        "outputId": "449e50a4-3b1e-4c53-e5ba-64ac2b500521"
      },
      "execution_count": 63,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[[0], 1]"
            ]
          },
          "metadata": {},
          "execution_count": 63
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "nested[0] = 'zero'\n",
        "original"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "75KBZZwO-QwD",
        "outputId": "885af992-dbad-4e4f-ae91-ee1f6e68c708"
      },
      "execution_count": 64,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[['zero'], 1]"
            ]
          },
          "metadata": {},
          "execution_count": 64
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "original[0][0] = 0\n",
        "nested"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GNPVbcCn-Tc7",
        "outputId": "ea7df37a-8b15-45a8-d91e-c4cd547a2a28"
      },
      "execution_count": 65,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[0]"
            ]
          },
          "metadata": {},
          "execution_count": 65
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "original"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "DHUkCORb-Vfb",
        "outputId": "5530d380-a124-4792-9901-95feccfc06ee"
      },
      "execution_count": 66,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[[0], 1]"
            ]
          },
          "metadata": {},
          "execution_count": 66
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "nested = [2]\n",
        "original\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LP5clUqG-W9P",
        "outputId": "df73bcd8-41b6-4e1c-db1e-fe7d4d6b3d50"
      },
      "execution_count": 67,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[[0], 1]"
            ]
          },
          "metadata": {},
          "execution_count": 67
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "original = [[0], 1]\n",
        "shallow = original[:]\n",
        "import copy\n",
        "deep = copy.deepcopy(original)"
      ],
      "metadata": {
        "id": "9FG9BdD7-hIy"
      },
      "execution_count": 68,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "shallow[1] = 2\n",
        "shallow"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uqCIMv0i-klI",
        "outputId": "b06a8387-b0fe-4426-b0b2-ba86baca6144"
      },
      "execution_count": 69,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[[0], 2]"
            ]
          },
          "metadata": {},
          "execution_count": 69
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "original"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tG2dnAUC-oSe",
        "outputId": "0f627bbc-b628-4eb9-923f-2eb6136c58ee"
      },
      "execution_count": 70,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[[0], 1]"
            ]
          },
          "metadata": {},
          "execution_count": 70
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "shallow[0][0] = 'zero'\n",
        "original"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BIGI9htr-py2",
        "outputId": "6e8f8e29-886f-45b3-9404-e93a22866a42"
      },
      "execution_count": 71,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[['zero'], 1]"
            ]
          },
          "metadata": {},
          "execution_count": 71
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "deep[0][0] = 5\n",
        "deep"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "htNkyjTs-sHi",
        "outputId": "3cbe1e7c-4572-4a71-d8b8-f7c45d27de0a"
      },
      "execution_count": 72,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[[5], 1]"
            ]
          },
          "metadata": {},
          "execution_count": 72
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "original"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_hZOjc-t-ueC",
        "outputId": "32dd99c0-436f-48a2-d236-c32d77c31b8d"
      },
      "execution_count": 73,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[['zero'], 1]"
            ]
          },
          "metadata": {},
          "execution_count": 73
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Try This: List copies\n",
        "Suppose that you have the following list: `x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]` What code could you use to get a copy y of that list in which you could change the elements without the side effect of changing the contents of x?\n"
      ],
      "metadata": {
        "id": "1Oj3eScAQZo4"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "d4hRTcXMQgum"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import copy\n",
        "x = [1, 2, 3, 4, 5]\n",
        "copy_x = copy.deepcopy(x)\n",
        "copy_x"
      ],
      "metadata": {
        "id": "pAF4Q7cVQhA4",
        "outputId": "8c3898b8-7bdf-4717-9871-8b0981ed315e",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 4, 5]"
            ]
          },
          "metadata": {},
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 5.7 Tuples"
      ],
      "metadata": {
        "id": "PO5oyPDJQ1ZN"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = ('a', 'b', 'c')\n",
        "x[2]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "MZFUzvjH-w24",
        "outputId": "0a9a3098-70be-4ab5-8e5e-d2c1977e94ba"
      },
      "execution_count": 74,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'c'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 74
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[1:]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AMUv1CkF-3ZT",
        "outputId": "fb20d61c-ea62-44e9-f032-26c8147c3a51"
      },
      "execution_count": 75,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "('b', 'c')"
            ]
          },
          "metadata": {},
          "execution_count": 75
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "len(x)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "CbCOr-qF-5No",
        "outputId": "982ec9f7-c5c5-4ff2-bc5f-beeae954c41b"
      },
      "execution_count": 76,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "3"
            ]
          },
          "metadata": {},
          "execution_count": 76
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "max(x)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "s9GrTDSn-69H",
        "outputId": "2becda7f-2fb5-4b47-922b-43aeb5a4aeac"
      },
      "execution_count": 77,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'c'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 77
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "min(x)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "_kdZkBfH-8tm",
        "outputId": "46cbe928-5b76-4651-de1a-80e744d60dab"
      },
      "execution_count": 78,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'a'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 78
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "5 in x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BOqUmdZY---1",
        "outputId": "20f15a35-cd38-4cef-daf5-3968d18fd6a2"
      },
      "execution_count": 79,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 79
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "5 not in x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "B-4oPNeU_AcX",
        "outputId": "de9b97b2-0d3a-4cb9-d462-93dcb6be0a5b"
      },
      "execution_count": 80,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 80
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[2] = 'd'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "hh-TFdWi_CUj",
        "outputId": "9329c354-abda-4240-a3d5-aeb22e64a39e"
      },
      "execution_count": 81,
      "outputs": [
        {
          "output_type": "error",
          "ename": "TypeError",
          "evalue": "'tuple' object does not support item assignment",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-81-dcc4b983047e>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'd'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x + x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ctT13uAg_Efz",
        "outputId": "c0c63491-7fa9-4c82-9157-869efb5f2552"
      },
      "execution_count": 82,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "('a', 'b', 'c', 'a', 'b', 'c')"
            ]
          },
          "metadata": {},
          "execution_count": 82
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "2 * x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-7xLl_bD_Hu3",
        "outputId": "40cb9810-c9de-4de6-bf07-c0c15a097510"
      },
      "execution_count": 83,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "('a', 'b', 'c', 'a', 'b', 'c')"
            ]
          },
          "metadata": {},
          "execution_count": 83
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x[:]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QHDeIaDw_OxM",
        "outputId": "b1a279fc-74d0-447f-e5d2-f5d0c150a72c"
      },
      "execution_count": 84,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "('a', 'b', 'c')"
            ]
          },
          "metadata": {},
          "execution_count": 84
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x * 1"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "23bjBiYc_Qlu",
        "outputId": "dd2cccc6-e2d6-4aed-e461-9d333c42e9c3"
      },
      "execution_count": 85,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "('a', 'b', 'c')"
            ]
          },
          "metadata": {},
          "execution_count": 85
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x + ()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WdX_--YJ_TT8",
        "outputId": "01b7c388-a681-4107-e7f8-03181c784607"
      },
      "execution_count": 86,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "('a', 'b', 'c')"
            ]
          },
          "metadata": {},
          "execution_count": 86
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = 3\n",
        "y = 4\n",
        "(x + y)   # This line adds x and y."
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "c3XxBI-e_VLm",
        "outputId": "fc29e2d0-02a1-49fa-fada-0669531ed3ec"
      },
      "execution_count": 87,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "7"
            ]
          },
          "metadata": {},
          "execution_count": 87
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "(x + y,)  # Including a comma indicates that the parentheses denote a tuple.\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "09Ckv_E4_YcT",
        "outputId": "a24fc442-b093-462a-c0d3-de8231c7b582"
      },
      "execution_count": 88,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(7,)"
            ]
          },
          "metadata": {},
          "execution_count": 88
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "()        # To create an empty tuple, use an empty pair of parentheses."
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lTZKhYMl_bRv",
        "outputId": "8bd55427-0dfb-443c-9946-b105475bbdb4"
      },
      "execution_count": 89,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "()"
            ]
          },
          "metadata": {},
          "execution_count": 89
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "(one, two, three, four) =  (1, 2, 3, 4)\n",
        "one"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "k4wdcAMe_dcP",
        "outputId": "86d0ec4f-ca9a-4adc-a3e2-34baca0a0964"
      },
      "execution_count": 90,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {},
          "execution_count": 90
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "two"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "trtmM38w_gGb",
        "outputId": "2eedd2ca-1ea5-413b-f45c-0f5eb7c345e3"
      },
      "execution_count": 91,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2"
            ]
          },
          "metadata": {},
          "execution_count": 91
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "one, two, three, four = 1, 2, 3, 4"
      ],
      "metadata": {
        "id": "5FDFWHst_h7b"
      },
      "execution_count": 92,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x = (1, 2, 3, 4)\n",
        "a, b, *c = x\n",
        "a, b, c"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3bs1H19p_kK7",
        "outputId": "e7582a65-c0ac-4ba7-dc6a-b871b19525ab"
      },
      "execution_count": 93,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(1, 2, [3, 4])"
            ]
          },
          "metadata": {},
          "execution_count": 93
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a, *b, c = x\n",
        "a, b, c"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EeYOFfZV_n0Y",
        "outputId": "f3a87b53-4a1a-4a17-c419-7f59ad8eae0c"
      },
      "execution_count": 94,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(1, [2, 3], 4)"
            ]
          },
          "metadata": {},
          "execution_count": 94
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "*a, b, c = x\n",
        "a, b, c"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f8hjmJx6_pq3",
        "outputId": "405c659d-05a2-4da5-d99c-684948a79ddb"
      },
      "execution_count": 95,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "([1, 2], 3, 4)"
            ]
          },
          "metadata": {},
          "execution_count": 95
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a, b, c, d, *e = x\n",
        "a, b, c, d, e"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jYcucbVA_rcb",
        "outputId": "e40872f5-adb7-41b1-8e9d-e8f5e47e486c"
      },
      "execution_count": 96,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(1, 2, 3, 4, [])"
            ]
          },
          "metadata": {},
          "execution_count": 96
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "[a, b] = [1, 2]\n",
        "[c, d] = 3, 4\n",
        "[e, f] = (5, 6)\n",
        "(g, h) = 7, 8\n",
        "i, j = [9, 10]\n",
        "k, l = (11, 12)\n",
        "a"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0gJepbCm_ty3",
        "outputId": "2e53800f-71f0-4665-a325-19b051c0650a"
      },
      "execution_count": 97,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {},
          "execution_count": 97
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "[b, c, d]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "rH7PcPvK_wxi",
        "outputId": "f29395ba-ec24-4ffc-bfc6-1a537a8dcd57"
      },
      "execution_count": 98,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[2, 3, 4]"
            ]
          },
          "metadata": {},
          "execution_count": 98
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "(e, f, g)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XTHghw1v_y-v",
        "outputId": "9449c273-412c-405b-bade-1343be6eeee3"
      },
      "execution_count": 99,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(5, 6, 7)"
            ]
          },
          "metadata": {},
          "execution_count": 99
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "h, i, j, k, l"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "UY1Jj3zA_1Ae",
        "outputId": "96c508f5-c1d2-40ef-9fa3-0a62f5060737"
      },
      "execution_count": 100,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(8, 9, 10, 11, 12)"
            ]
          },
          "metadata": {},
          "execution_count": 100
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "list((1, 2, 3, 4))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "K4UYOqKp_2mH",
        "outputId": "c1c21117-79c0-4bf7-c6f0-f2e28bf7f6f8"
      },
      "execution_count": 101,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 4]"
            ]
          },
          "metadata": {},
          "execution_count": 101
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "tuple([1, 2, 3, 4])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "METAQ2tA_4w7",
        "outputId": "a2e8bf9a-2bbe-4430-d5ff-fd0b669575a4"
      },
      "execution_count": 102,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(1, 2, 3, 4)"
            ]
          },
          "metadata": {},
          "execution_count": 102
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "list(\"Hello\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JOF0EHMM_6_E",
        "outputId": "768ce87b-8d07-4c56-d550-53ac44fe9502"
      },
      "execution_count": 103,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['H', 'e', 'l', 'l', 'o']"
            ]
          },
          "metadata": {},
          "execution_count": 103
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: Tuples\n",
        "Explain why the following operations aren't legal for the tuple `x = (1, 2, 3, 4)`:\n",
        "\n",
        "`x.append(1)`\n",
        "\n",
        "`x[1] = \"hello\"`\n",
        "\n",
        "`del x[2]`\n",
        "\n"
      ],
      "metadata": {
        "id": "XtbL0ijHRM50"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "All of these operations change the object in place, and tuples can't be changed.\n"
      ],
      "metadata": {
        "id": "-vZdrAWURhm9"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "If you had a tuple `x = (3, 1, 4, 2)`, how might you end up with x sorted?\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "5p8RCWsKTXJD"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "1D4rsV8yRlSn"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x = (3, 1, 4, 2)\n",
        "x = tuple(sorted(x))\n",
        "x"
      ],
      "metadata": {
        "id": "Vjr7wXcYTNiT",
        "outputId": "b267d97e-8346-462a-a476-8743336f9db2",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(1, 2, 3, 4)"
            ]
          },
          "metadata": {},
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 5.8 Sets"
      ],
      "metadata": {
        "id": "_060b9vSRl0T"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = set([1, 2, 3, 1, 3, 5])    #A\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TZ3w2VcS_86w",
        "outputId": "b254e3fd-1934-4227-d9c0-2cbbf124adce"
      },
      "execution_count": 104,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{1, 2, 3, 5}"
            ]
          },
          "metadata": {},
          "execution_count": 104
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.add(6)              #C\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mpDc6UN1ABRP",
        "outputId": "613ada24-828b-4e8c-f9b5-906753aaac0d"
      },
      "execution_count": 105,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{1, 2, 3, 5, 6}"
            ]
          },
          "metadata": {},
          "execution_count": 105
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.remove(5)                    #D\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MJcWmQS-ADHX",
        "outputId": "3aa0ff76-204d-43e0-af46-54df4bbff644"
      },
      "execution_count": 106,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{1, 2, 3, 6}"
            ]
          },
          "metadata": {},
          "execution_count": 106
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "1 in x        #E"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fU58BoycAEzo",
        "outputId": "c5f4190f-0aca-465a-9456-935640b7bfcf"
      },
      "execution_count": 107,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 107
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "4 in x        #E"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TT69PqgCAGeM",
        "outputId": "ef5fb47d-5c40-4f6e-82b6-c88b36166ce4"
      },
      "execution_count": 108,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 108
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "y = set([1, 7, 8, 9])\n",
        "x | y                #F"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3MDsgF5uAIef",
        "outputId": "beab8a6c-8970-49d5-8d19-aa51e6629fe1"
      },
      "execution_count": 109,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{1, 2, 3, 6, 7, 8, 9}"
            ]
          },
          "metadata": {},
          "execution_count": 109
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x & y                  #G"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4saQzt29AKXG",
        "outputId": "abbfe820-5762-4999-9167-fa696c9f4ab8"
      },
      "execution_count": 110,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{1}"
            ]
          },
          "metadata": {},
          "execution_count": 110
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x ^ y                  #H"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-3qL57A9AMCn",
        "outputId": "f43bbe72-aaa0-4ee6-dcfd-9bd3d0fc52e9"
      },
      "execution_count": 111,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{2, 3, 6, 7, 8, 9}"
            ]
          },
          "metadata": {},
          "execution_count": 111
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x = set([1, 2, 3, 1, 3, 5])\n",
        "z = frozenset(x)\n",
        "z"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "p6GElp9-AOGI",
        "outputId": "7ce4395a-668f-407d-bb41-068bfa8dc7c2"
      },
      "execution_count": 112,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "frozenset({1, 2, 3, 5})"
            ]
          },
          "metadata": {},
          "execution_count": 112
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "z.add(6)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "9ScTMQoCARSs",
        "outputId": "679bb489-87ee-4b3e-b4c1-35f5bb38c408"
      },
      "execution_count": 113,
      "outputs": [
        {
          "output_type": "error",
          "ename": "AttributeError",
          "evalue": "'frozenset' object has no attribute 'add'",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-113-60917eecf5a3>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mz\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mAttributeError\u001b[0m: 'frozenset' object has no attribute 'add'"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.add(z)\n",
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8Lnz7NvEAT3T",
        "outputId": "af1a0f42-72a0-42be-e844-2c60e9edecb2"
      },
      "execution_count": 114,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{1, 2, 3, 5, frozenset({1, 2, 3, 5})}"
            ]
          },
          "metadata": {},
          "execution_count": 114
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: Sets\n",
        "If you were to construct a set from the following list, how many elements would the set have?: `[1, 2, 5, 1, 0, 2, 3, 1, 1, (1, 2, 3)]`\n"
      ],
      "metadata": {
        "id": "n_ugHjiCRtww"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "bo58OK-yAXDg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Six unique elements: 1, 2, 5, 0, 3, and the tuple (1, 2, 3)\n",
        "Lab 5: Examining a List"
      ],
      "metadata": {
        "id": "Na5o99AATw4w"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Lab 5: Examining a List\n",
        "In this lab, the task is to read a set of temperature data (the monthly high temperatures at Heathrow Airport for 1948 through 2016) from a file and then find some basic information: the highest and lowest temperatures, the mean (average) temperature, and the median temperature (the temperature in the middle if all the temperatures are sorted).\n",
        "\n",
        "The temperature data is in the file lab_05.txt in the source code directory for this chapter. Because I haven't yet discussed reading files, here's the code to read the files into a list:\n",
        "```\n",
        "temperatures = []\n",
        "with open('lab_05.txt') as infile:\n",
        "     for row in infile:\n",
        "        temperatures.append(float(row.strip()))\n",
        "```\n",
        "You should find the highest and lowest temperature, the average, and the median. You'll probably want to use the `min()`, `max()`, `sum()`, `len()`, and `sort()` functions/methods."
      ],
      "metadata": {
        "id": "HXZTv1GmR0me"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "temperatures = []\n",
        "\n",
        "with open('lab_05.txt') as infile:\n",
        "     for row in infile:\n",
        "        temperatures.append(float(row.strip()))\n",
        "\n",
        "max_temp = max(temperatures)\n",
        "min_temp = min(temperatures)\n",
        "mean_temp = sum(temperatures)/len(temperatures)\n",
        "# we'll need to sort to get the median temp\n",
        "temperatures.sort()\n",
        "median_temp = temperatures[len(temperatures)//2]\n",
        "print(\"max = {}\".format(max_temp))\n",
        "print(\"min = {}\".format(min_temp))\n",
        "print(\"mean = {}\".format(mean_temp))\n",
        "print(\"median = {}\".format(median_temp))\n"
      ],
      "metadata": {
        "id": "qov57_GHSMRQ",
        "outputId": "2cfc064e-bc7d-4cef-b04e-4eff012c202d",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "max = 28.2\n",
            "min = 0.8\n",
            "mean = 14.848309178743966\n",
            "median = 14.7\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# prompt: read a set of temperature data from a file lab05.txt and then find the\n",
        "#         highest and lowest temperatures, the mean temperature, and the median temperature .\n",
        "\n",
        "temperatures = []\n",
        "with open('lab_05.txt') as infile:\n",
        "     for row in infile:\n",
        "        temperatures.append(float(row.strip()))\n",
        "\n",
        "# Find the highest and lowest temperature\n",
        "highest_temp = max(temperatures)\n",
        "lowest_temp = min(temperatures)\n",
        "\n",
        "# Find the mean temperature\n",
        "mean_temp = sum(temperatures) / len(temperatures)\n",
        "\n",
        "# Find the median temperature\n",
        "temperatures.sort()\n",
        "if len(temperatures) % 2 == 0:\n",
        "    median_temp = (temperatures[len(temperatures) // 2] + temperatures[len(temperatures) // 2 - 1]) / 2\n",
        "else:\n",
        "    median_temp = temperatures[len(temperatures) // 2]\n",
        "\n",
        "# Print the results\n",
        "print(\"Highest temperature:\", highest_temp)\n",
        "print(\"Lowest temperature:\", lowest_temp)\n",
        "print(\"Mean temperature:\", mean_temp)\n",
        "print(\"Median temperature:\", median_temp)\n"
      ],
      "metadata": {
        "id": "bwTUuYngSLkK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Bonus\n",
        "Determine how many unique temperatures are in the list.\n"
      ],
      "metadata": {
        "id": "9IJncX9VSMvZ"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "sF0Qv9b7SSOU"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "unique_temps = len(set(temperatures))\n",
        "\n",
        "print(\"number of temps - {}\".format(len(temperatures)))\n",
        "print(\"number of unique temps - {}\".format(unique_temps))\n"
      ],
      "metadata": {
        "id": "hSPobsfzST7g",
        "outputId": "c454c574-3ab5-47b0-cbc8-8f6d3c2584f6",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "number of temps - 828\n",
            "number of unique temps - 217\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# prompt: Determine how many unique temperatures are in the list.\n",
        "\n",
        "unique_temps = len(set(temperatures))\n",
        "\n",
        "print(\"number of temps - {}\".format(len(temperatures)))\n",
        "print(\"number of unique temps - {}\".format(unique_temps))\n"
      ],
      "metadata": {
        "id": "PsChtys5YkJv"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}